//
//  FindViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/6/22.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "FindViewController.h"
#import "IGSearchVC.h"
#import "FindCollectionReusableView.h"
#import "ArtistTableVC.h"
#import "ArtInstitutionTableViC.h"
#import "HotClassDetailVC.h"
#import "ZTXQViewController.h"
#import "StoreCollectionVC.h"
#import "FindViewCell_first.h"
#import "FindViewCell_second.h"
#import "FindViewCell_thrid.h"
#import "IGFindApi.h"
@interface FindViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    NSArray *firstArr;
    NSArray *sectionArr;
    NSArray *imageTitleArr;
}
@property (nonatomic, strong)UICollectionView *collectionV;
@property (nonatomic, strong)NSMutableArray *list;//专题列表
@property (nonatomic, strong)NSMutableArray *productArr;//分类列表
@end

@implementation FindViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self creatNav];
    self.list = [@[] mutableCopy];
    self.productArr = [@[] mutableCopy];
    
    [self ctreatCollectionView];
    AdjustsScrollViewInsetNever(self, self.collectionV);
    
    [self.collectionV reloadData];
    [self loadData];
}

- (void)creatNav
{
//    self.title = @"发现";
    self.title = NSLocalizedString(@"Explore", nil);

    [self ig_setupRightButtonItemWithTitle:nil image:[UIImage imageNamed:@"app_search_icon"] highLightedImage:[UIImage imageNamed:@"app_search_icon"]];
}

- (void)ig_rightButtonPressed:(id)sender
{
    [UIViewController ig_pushViewControllerClass:[IGSearchVC class] animated:YES];
}

- (void)loadData
{
    @KWeakObj(self)
    
    NSString *klanguage = [[NSUserDefaults standardUserDefaults] objectForKey:KLanguage];
    if ([klanguage containsString:@"zh-Hans"]) {
        firstArr = @[NSLocalizedString(@"Artists",nil), NSLocalizedString(@"Art Institutions",nil), NSLocalizedString(@"Mall", nil)];
    } else {
        firstArr = @[NSLocalizedString(@"Artists",nil), NSLocalizedString(@"Art Institutions",nil)];
    }
    imageTitleArr = @[@"艺术家", @"艺术机构", @"商城"];
//    sectionArr = @[@"", @"热门分类", @"专题"];
    sectionArr = @[@"", NSLocalizedString(@"Hot Categories", nil), NSLocalizedString(@"Feature", nil)];
    IGFindApi *api = [IGFindApi new];
    [api startWithDecodeClass:[IGFindResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.collectionV ig_endHeaderRefreshing];
        if (responseObject)
        {
            IGFindResp *resp = responseObject;
            IGFindModel *model = resp.data;
            if (model.list && model.list.count > 0)
            {
                self.list = [NSMutableArray arrayWithArray:model.list];
            }
            if (model.productTypes && model.productTypes.count > 0)
            {
                self.productArr = [NSMutableArray arrayWithArray:model.productTypes];
            }
            [selfWeak.collectionV reloadData];
        }
    }];
}

- (void)ctreatCollectionView
{
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavigationMagin, KSCREENWIDTH, KSCREENHEIGHT - NavigationMagin - 49) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collectionV.backgroundColor = [UIColor c3Color];
    self.collectionV.alwaysBounceVertical = YES;
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"FindViewCell_first" bundle:nil] forCellWithReuseIdentifier:@"FindViewCell_first"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"FindViewCell_second" bundle:nil] forCellWithReuseIdentifier:@"FindViewCell_second"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"FindViewCell_thrid" bundle:nil] forCellWithReuseIdentifier:@"FindViewCell_thrid"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"FindCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FindCollectionReusableView"];
    @KWeakObj(self);
    [self.collectionV ig_setHeaderWithRefreshingBlock:^{
        [selfWeak loadData];
    }];
    [self.view addSubview:self.collectionV];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.productArr.count > 0 ? (self.list.count > 0 ? 3: 2) : 1;
//    1+((self.productArr.count>0&&self.list.count>0)?2:0);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return firstArr.count;
    }
    else if (section == 1)
    {
        return _productArr.count>0?_productArr.count:0;
    }
    return _list.count>0?_list.count : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        FindViewCell_first *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindViewCell_first" forIndexPath:indexPath];
        NSString *str = firstArr[indexPath.item];
//        cell.imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", str]];
        cell.imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", imageTitleArr[indexPath.item]]];
        cell.titleL.text = str;
        return cell;
    } else if (indexPath.section == 1) {
        FindViewCell_second *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindViewCell_second" forIndexPath:indexPath];
        NSDictionary *dic = _productArr[indexPath.item];
        if (dic)  {
            [cell.imagV sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
            cell.titleL.text = dic[@"title"];
        }
        return cell;
    } else if (indexPath.section == 2) {
        FindViewCell_thrid *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindViewCell_thrid" forIndexPath:indexPath];
        NSDictionary *dic = _list[indexPath.item];
        if (dic) {
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
            cell.titleL.text = dic[@"title"];
            cell.subL.text = dic[@"subTitle"];
        }
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        //艺术家
        if (indexPath.item == 0)
        {
            [UIViewController ig_pushViewController:[[ArtistTableVC alloc] init] animated:YES];
            return;
        }
        //艺术机构
        if (indexPath.item == 1)
        {
            [UIViewController ig_pushViewController:[[ArtInstitutionTableViC alloc] init] animated:YES];
            return;
        }
        //商城
        if (indexPath.item == 2)
        {
            [UIViewController ig_pushViewController:[[StoreCollectionVC alloc] init] animated:YES];
            return;
        }
    }
    else if (indexPath.section == 1)
    {
        NSDictionary *dic = _productArr[indexPath.item];
        HotClassDetailVC *vc = [[HotClassDetailVC alloc] init];
        vc.type = @"classType";
        vc.value = dic[@"id"];
        vc.title = dic[@"title"];
        [UIViewController ig_pushViewController:vc animated:YES];
    }
    else if (indexPath.section == 2)
    {
        NSDictionary *dic = _list[indexPath.item];
        ZTXQViewController *vc = [[ZTXQViewController alloc] init];
        vc.s_id = dic[@"id"];
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
//        return CGSizeMake(55, 122);
        return CGSizeMake(100, 122);
    }
    else if (indexPath.section == 1)
    {
        return CGSizeMake(KSCREENWIDTH / 4, (KSCREENWIDTH / 4 - 30) * 19 / 13 + 55);
    }
    return CGSizeMake(KSCREENWIDTH ,(KSCREENWIDTH - 30) * 9 / 16 + 95);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 || indexPath.section == 2)
    {
        FindCollectionReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FindCollectionReusableView" forIndexPath:indexPath];
        reusableV.titleL.text = sectionArr[indexPath.section];
        reusableV.index = indexPath.section;
        return reusableV;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1 || section == 2)
    {
        return CGSizeMake(KSCREENWIDTH, 75);
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0)
    {
//        return UIEdgeInsetsMake(0, (KSCREENWIDTH - 55 * 3) / 4 - 10, 0, (KSCREENWIDTH - 55 * 3) / 4 - 10);
        return UIEdgeInsetsMake(0, (KSCREENWIDTH - 100 * 3) / 4 - 10, 0, (KSCREENWIDTH - 100 * 3) / 4 - 10);
    }
    return UIEdgeInsetsZero;
}

//cell的最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 0)
    {
//        return (KSCREENWIDTH - 55 * 3) / 4;
        return (KSCREENWIDTH - 100 * 3) / 4;

    }
    return 0;
}

//cell的最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
